package com.ys.util
{
	import com.ys.obj.Text;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	import gs.easing.Quad;
	
	public class GraphicsUtil
	{
		public static function createBorder(w:int, h:int, color:uint = 0xaaaaaa):Sprite
		{			
			var bg:Sprite = new Sprite;
			bg.graphics.beginFill(color);
			bg.graphics.drawRect(0, 0, w-1, h-1);
			return(bg);
		}
		
		public static function createBorderLine(w:int, h:int, color:uint = 0xaaaaaa):Sprite
		{			
			var bg:Sprite = new Sprite;
			bg.graphics.lineStyle(1, color);
			bg.graphics.drawRect(0, 0, w-1, h-1);
			return(bg);
		}
		
		public static function drawCircularButton(r:Number, color:uint, txt:String=null):Sprite
		{
			var spr:Sprite = new Sprite;
			spr.graphics.beginFill(color, 1);
			spr.graphics.drawCircle(0, 0, r/2);
			
			spr.buttonMode = spr.useHandCursor = true;
			
			spr.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			spr.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			
			if (txt)
			{
				var text:Text = new Text;
				text.setProperties(12, 0xffffff)
				text.text = txt.toUpperCase();
				text.centerPosition();
				spr.addChild(text);
			}
			
			function onRollOver(e:MouseEvent):void
			{
				reset();
				spr.graphics.lineStyle(2, Const.HIGHIGHT_COLOR);
				spr.graphics.drawCircle(0, 0, r/2);
			}
			
			function onRollOut(e:MouseEvent):void
			{
				reset();
				spr.graphics.drawCircle(0, 0, r/2);
			}
			
			function reset():void
			{
				spr.graphics.clear();
				spr.graphics.beginFill(color, 1);
			}
			
			return(spr);
		}
		
		
		public static function createAssetSprite(ast:Class, center:Boolean = true):Sprite
		{
			var asset:Sprite = new ast();
			if (center)
			{
				asset.x = -asset.width/2;
				asset.y = -asset.height/2;
			}
			return(asset);
		}
		
		public static function createPop(parent:Sprite, size:Number, color:uint, alpha:Number=0.7):Sprite
		{
			var spr:Sprite = new Sprite;
			spr.graphics.lineStyle(8, color, alpha);
			spr.graphics.drawCircle(0, 0, size);
			spr.scaleX = spr.scaleY = 0.1;
			TweenLite.to(spr, Const.DUR_SUMMON+1, {ease:Quad.easeOut, scaleX:2, scaleY:2, alpha:0, onComplete:onComplete});
			
			parent.addChild(spr);
			
			function onComplete():void
			{
				parent.removeChild(spr);
			}
			return(spr);
		}
		
		public static function createLine(parent:Sprite, h:Number):Sprite
		{
			var spr:Sprite = new Sprite;
			spr.graphics.lineStyle(30, Const.PALLET_COLOR, 0.4, false, 'normal', 'none');
			spr.graphics.moveTo(0, 0);
			spr.graphics.lineTo(0, h);
			TweenLite.to(spr, Const.DUR_SOUNDWAVE*2, {ease:Quad.easeOut, alpha:0, onComplete:onComplete});
			
			parent.addChild(spr);
			
			function onComplete():void
			{
				parent.removeChild(spr);
			}
			return(spr);
		}
		
		public static function createEffect(parent:Sprite):Sprite
		{			
			var offsetX:Number;
			var offsetY:Number;
			var offsetValue:Number = 50;
			
			var spr:Sprite = new Sprite;
			
			for (var i:int = 0; i < 4; i++)
			{
				offsetX = -offsetValue + Math.random()*offsetValue*2;
				offsetY = -offsetValue + Math.random()*offsetValue*2;
				createEffectChild(spr, Math.random()*50+10, i*0.1, offsetX, offsetY);
			}
			
			spr.rotation = -80;
			TweenLite.to(spr, Const.DUR_SUMMON+0.3, {ease:Quad.easeOut, rotation:0, onComplete:onComplete});
				
			parent.addChild(spr);
			
			function onComplete():void
			{
				parent.removeChild(spr);
			}
			
			return(spr);
		}
		
		public static function createEffectChild(parent:Sprite, size:Number, delay:Number, x:Number, y:Number):void
		{
			var spr:Sprite = new Sprite;
			spr.graphics.beginFill(Const.HIGHIGHT_COLOR, 0.7);
			spr.graphics.drawCircle(0, 0, size);
			spr.scaleX = spr.scaleY = 0;
			spr.x = x;
			spr.y = y;
			spr.scaleX = spr.scaleY = 0;
			TweenLite.to(spr, Const.DUR_SUMMON, {ease:Quad.easeOut, scaleX:1, scaleY:1, alpha:0, onComplete:onComplete});
			
			parent.addChild(spr);
			
			function onComplete():void
			{
				parent.removeChild(spr);
			}
		}
	}
}